<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增用户</div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="form.uname" placeholder="请输入用户名" style="width: 175px"></el-input>
      </el-form-item><br/>
      <el-form-item label="密码">
        <el-input placeholder="请输入密码" show-password  v-model="form.upwd" style="width: 175px"></el-input>
      </el-form-item><br/>
      <el-form-item label="手机号">
        <el-input v-model="form.utell" placeholder="请输入手机号" style="width: 175px"></el-input>
      </el-form-item><br/>
      <el-form-item label="会员等级">
        <el-input v-model="form.urole" placeholder="请输入会员等级" style="width: 175px"></el-input>
      </el-form-item><br/>
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
      <el-button type="warning" @click="reset" size="medium">重置</el-button>

    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'AddUser',
  data() {
    const checkPhone = (rule, value, callback) => {
      if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
        callback(new Error('请输入合法的手机号'));
      }
      callback()
    };
    return {
      form: {
        uname:'',
        upwd:'',
        utell:'',
        urole:''
      },
      rules: {
        uname: [
          { required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        utell: [
          { validator: checkPhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/user/save', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功')
              this.$refs['ruleForm'].resetFields()
              this.$router.push("/userList")
            } else {
              this.$notify.error(res.msg)
            }
          })
        }
      })
    },
    reset(){
      this.form = this.$options.data().form
    }
  }
}

</script>

